<template>
  <div class="login-wrap">
    <el-card class="login-container">
      <div class="login-title" slot="header">XXX管理系统</div>
      <!-- 登录表单 -->
      <el-form ref="loginForm" :model="info" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="info.username"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <label slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
          <el-input
            v-model="info.password"
            placeholder="请输入密码"
            clearable
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="login">登录</el-button>
          <!-- <el-button @click="clearvalue">重置</el-button> -->
          <el-button @click="regist">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import router from '@/router'
export default {
  name: 'Login',
  data() {
    return {
      info: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    }
  },
  methods: {
    // 需求: 
    // 1, 用户名在数据库中,输入正确密码即可跳转到后台
    // 2, 用户名不在数据库中, 点击登录跳转到游客界面
    login() {
      // 通过记录登录状态进行页面跳转
      if (this.info.username == 'admin' && this.info.password == '123') {
        //登录成功,记录状态
        localStorage.setItem('isLogin', 1)
        alert('登录成功')
        //跳转后台
        this.$router.push('/dashboard')
      } else {
        alert('用户名或密码错误,请重新输入')
        localStorage.setItem('isLogin', 0)
        this.info.username = ''
        this.info.password = ''
        return
      }
      // 改造导航守卫
      router.beforeEach((to, from, next) => {
        // to:到那个path去
        // from: 从哪个path来
        // next(): 是否可以通行

        let isLogin = localStorage.getItem('isLogin') === '1' ? true : false
        if (isLogin == true) {
          if (to.path == '/login') {
            // 不需要再次登录,跳转到/路由
            next({ path: '/' })
          } else {
            // 直接放行
            next()
          }
        } else {
          // 没有登录只能访问/login
          if (to.path != '/login') {
            alert('请登录')
            next('/login')
          } else {
            next()
          }
        }
      })
      /*   /// 校验表单
        // this.$refs['loginForm']得到form对象
        console.log(this.$refs['loginForm'])
      this.$refs['loginForm'].validate((valid) => {
        //valid为true: 表单验证通过
        if (valid) {
          //发送post请求,对比用户名和密码
          //提示登录成功信息
          this.$message({
            message: '登录成功',
            type: 'success',
            showClose: true,
            duration: 1000,
          })
          // 方式二
          // this.$message.success('登录成功')

          //跳转到后台首页
          this.$router.push('/dashboard')
        }
      }) */
    },
    clearvalue() {
      if (confirm('确定重置信息嘛?')) {
        this.info.username = ''
        this.info.password = ''
        return
      }
    },
    regist(){

    }
  },
}
</script>

<style>
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #27304f;
}
.login-container {
  width: 400px;
  /* height: 600px; */
  background-color: #fff;
}
.login-title {
  text-align: center;
}
</style>
